package ace.module.captcha.core.impl.manager.repository.impl;

import ace.module.captcha.core.impl.dao.entity.CaptchaComponentConfig;
import ace.module.captcha.core.impl.dao.repository.CaptchaComponentConfigRepository;
import ace.module.captcha.core.impl.manager.repository.CaptchaComponentConfigMgrRepository;
import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/3/21 9:06
 */
@AllArgsConstructor
@Component
public class CaptchaComponentConfigMgrRepositoryImpl
    implements CaptchaComponentConfigMgrRepository {
  public static final String CACHE_AREA = "captcha_component_config";
  public static final String CACHE_CAPTCHA_ITEM = "captcha:component:config";
  private final CaptchaComponentConfigRepository captchaComponentConfigRepository;

  @Cached(area = CACHE_AREA, name = CACHE_CAPTCHA_ITEM, cacheType = CacheType.BOTH, key = "args[0]")
  @Override
  public CaptchaComponentConfig getById(Long id) {
    return this.captchaComponentConfigRepository.getById(id);
  }

  @CacheUpdate(area = CACHE_AREA, name = CACHE_CAPTCHA_ITEM, key = "args[0].id", value = "#result")
  @Override
  public CaptchaComponentConfig save(CaptchaComponentConfig componentConfig) {
    return this.captchaComponentConfigRepository.save(componentConfig);
  }

  @CacheInvalidate(area = CACHE_AREA, name = CACHE_CAPTCHA_ITEM, key = "args[0]")
  @Override
  public void remove(Long id) {
    this.captchaComponentConfigRepository.deleteById(id);
  }
}
